<template>
  <div class="admin-login-bg">
    <div class="admin-login-container">
      <div class="admin-login-header">
        <div class="logo">
          <div class="logo-icon">
            <i class="fas fa-hospital"></i>
          </div>
          <div class="logo-text">安心医疗后台</div>
        </div>
        <div class="login-title">管理员登录</div>
        <div class="login-subtitle">请使用管理员账号登录后台管理系统</div>
      </div>
      <el-form class="admin-login-form" :model="form" @submit.prevent>
        <el-form-item>
          <el-input 
            v-model="form.username" 
            placeholder="请输入管理员账号" 
            prefix-icon="el-icon-user" 
            size="large" 
          />
        </el-form-item>
        <el-form-item>
          <el-input 
            v-model="form.password" 
            type="password" 
            placeholder="请输入密码" 
            prefix-icon="el-icon-lock" 
            size="large" 
          />
        </el-form-item>
        <el-form-item>
          <el-button 
            class="login-btn" 
            type="primary" 
            size="large" 
            style="width:100%" 
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../../stores/userStore'
import { useAppStore } from '../../stores/appStore'
import { loginApi,getUserInfoApi } from '@/api/user.ts'

const router = useRouter()
const userStore = useUserStore()
const appStore = useAppStore()
const form = ref({ username: '', password: '' })

async function handleLogin() {
  if (!form.value.username || !form.value.password) {
    ElMessage.warning('请输入账号和密码')
    return
  }
  
  try {
    const loginRes = await loginApi(form.value.username, form.value.password)
    
    if (loginRes?.code === 200 && loginRes.data) {
      const token  = loginRes.data
      localStorage.setItem('token', token)
      const userInfo = await getUserInfoApi()
      if (!userInfo.data.authorities || !userInfo.data.authorities.includes('ROLE_ADMIN')) {
        ElMessage.error('无权限，只有管理员账号可登录后台')
        return
      }
      userStore.login({
        name: userInfo.data.username,
        role: 'ADMIN',
        avatar: userInfo.data.avatar || ''
      })
      appStore.login(userInfo.data.username, token, userInfo.data.avatar || '')

      ElMessage.success('登录成功')
      router.push('/admin')
    } else {
      ElMessage.error(loginRes?.msg || '登录失败')
    }
  } catch (e: any) {
    const errorMsg = e.message || e.msg || '登录失败'
    ElMessage.error(errorMsg)
    console.error('登录错误:', e)
  }
}
</script>

<style scoped>
/* 样式保持不变 */
.admin-login-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #e6f0ff 0%, #f0f4f8 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.4s;
}

.admin-login-container {
  width: 400px;
  background: #fff;
  border-radius: 22px;
  box-shadow: 0 4px 32px rgba(13, 27, 42, 0.10);
  padding: 48px 36px 36px 36px;
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  transition: background 0.4s, box-shadow 0.4s;
}

.admin-login-header {
  text-align: center;
  margin-bottom: 32px;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 10px;
}

.logo-icon {
  width: 54px;
  height: 54px;
  background: linear-gradient(135deg, #0d6efd, #0dcaf0);
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 28px;
  box-shadow: 0 2px 8px rgba(13, 110, 253, 0.10);
}

.logo-text {
  font-family: 'Poppins', 'Noto Sans SC', sans-serif;
  font-weight: 700;
  font-size: 24px;
  color: #0d6efd;
}

.login-title {
  font-size: 22px;
  font-weight: 600;
  color: #232946;
  margin-bottom: 4px;
  letter-spacing: 1px;
}

.login-subtitle {
  font-size: 15px;
  color: #6c757d;
  margin-bottom: 0;
}

.admin-login-form {
  width: 100%;
  margin-top: 10px;
}

.el-input__wrapper {
  border-radius: 12px !important;
  box-shadow: 0 1px 4px rgba(13, 110, 253, 0.04);
  font-size: 16px;
}

.el-input__inner {
  font-size: 16px;
  padding: 12px 16px;
}

.login-btn {
  background: linear-gradient(90deg, #0d6efd 0%, #0dcaf0 100%);
  border: none;
  font-size: 18px;
  font-weight: 600;
  border-radius: 24px;
  box-shadow: 0 2px 8px rgba(13, 110, 253, 0.10);
  transition: background 0.3s, box-shadow 0.3s;
}

.login-btn:hover {
  background: linear-gradient(90deg, #0dcaf0 0%, #0d6efd 100%);
  box-shadow: 0 4px 16px rgba(13, 110, 253, 0.18);
}
</style>